package com.example.speeddating.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.entity.Follow;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.speeddating.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Mapper
public interface FollowMapper extends BaseMapper<Follow> {


    /**
     * 未读粉丝数量
     */
    @Select(value = "SELECT count(*) FROM follow WHERE follow_id = #{userId} AND deleted = 0 AND is_read = 0")
    public Integer selectUnReadCount(Long userId);

    /**
     * 更新未读
     * @param userId
     * @return
     */
    @Update(value = "update follow set is_read = 1 where follow_id = #{userId} and is_read = 0 and deleted = 0")
    public int updateFollow(Long userId);

    /**
     * 粉丝数量
     */
    @Select(value = "SELECT count(*) FROM follow WHERE follow_id = #{userId} AND deleted = 0")
    public Integer fansCount(Long userId);

    /**
     * 查询关注的粉丝
     * @param page
     * @param userId
     * @return
     */
    @Select(value = "select u.* from `follow` f JOIN `user` u ON u.id = f.user_id WHERE f.follow_id = #{userId}")
    Page<User> selectFollowUser(Page<User> page, Long userId);


    /**
     * 更新未读粉丝数量
     * @param userId
     */
    @Update("update `follow` set is_read = 1 where follow_id = #{userId} and is_read = 0 and deleted = 0")
    public void updateFollowUser(Long userId);


    /**
     * 插入
     * @param follow
     * @return
     */
    @Insert("insert into `follow` values(0,#{userId},0,1,#{followId},0)")
    int insertFollow(Follow follow);
}
